package com.powater.assetmanagementofxinyu.service.userserviceimlp;


import com.powater.assetmanagementofxinyu.dao.UserMapper;
import com.powater.assetmanagementofxinyu.dto.userinfo.LoginMessageDto;
import com.powater.assetmanagementofxinyu.enums.LoginEnum;
import com.powater.assetmanagementofxinyu.pojo.User;
import com.powater.assetmanagementofxinyu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Long userid) {
        return userMapper.deleteByPrimaryKey(userid);
    }

    /**
     *
     * @param userbean 当前登录用户
     * @param user  需要添加的用户信息
     * @return 0：无权限 1：成功  2：用户名已存在
     */
    @Override
    public int insert(User userbean, User user) {
        if (userMapper.queryPermsById(userbean.getUserid())!="admin"){
            return 0;
        }else if (userMapper.queryNameById(userbean.getUserid())!=null){
            return 2;
        }
        userMapper.insert(user);
        return 0;
    }
    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public User selectByPrimaryKey(Long userid) {
        return userMapper.selectByPrimaryKey(userid);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<User> queryAllUser() {
        return userMapper.queryAllUser();
    }

    /**
     * 校验信息并且登录
     * @param loginMessageDto 储蓄登录信息
     * @param Request
     * @return
     */

    @Override
    public int checkUser(LoginMessageDto loginMessageDto, HttpServletRequest Request) {
        String username=loginMessageDto.getUsername();
        if (userMapper.selectNumByLogin(loginMessageDto)==0){
            return LoginEnum.USER_NULL.getCode();
        }
        if (!(userMapper.selectPasswordByName(username).equals(loginMessageDto.getUserpassword()))){
            return LoginEnum.PASSWORD_ERROR.getCode();
        }
        User user=userMapper.selectUserByName(loginMessageDto.getUsername());
        Request.getSession().setAttribute("userbean",user);
        return LoginEnum.SUCCESS.getCode();

    }

    /**
     *
     * @param userbean 当前登录用户
     * @param userid 需要删除的用户信息
     * @return 0:无权限 1：成功 2:不存在此人
     */
    @Override
    public int deleteUser(User userbean, Long userid) {
        if (userMapper.queryPermsById(userbean.getUserid())!="admin"){
            return 0;
        }else if (userMapper.queryNameById(userid)==null){
            return 2;
        }userMapper.addDeleteInfo(userMapper.selectByPrimaryKey(userid));
        userMapper.deleteByPrimaryKey(userid);
        return 1;
    }

}
